﻿// .h include
#include "httphelper.h"
#include "userhelper.h"

// Qt lib import
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QNetworkReply>


// Windows lib import
#include <qt_windows.h>

// Project lib import

QString httpDomain = "https://xt.bobahappy.com";

// IMHttpRequest
void IMHttpRequest::setHeader(const QString &key, const QString &value)
{
    header_[key]=value;
}

bool IMHttpRequest::requestHeaderProvider(
        QNetworkRequest &request )
{
    if(!UserHelper::instance()->token().isEmpty())
    {
        request.setRawHeader("X-Token", UserHelper::instance()->token().toUtf8());
    }
//    request.setRawHeader("Referer", "https://123.127.228.197");
    if(header_.size()>0)
    {
        for(auto each : header_.toStdMap()) {
            request.setRawHeader(each.first.toUtf8(), each.second.toUtf8());
        }
    }

    if ( enabledLog() )
    {
        foreach (auto h, request.rawHeaderList()) {
            qDebug()<<"===requestData===headers====="<<h<<":"<<request.rawHeader(h);
        }
    }

    return true;
}

bool IMHttpRequest::requestUrlSplicer(
        QNetworkRequest &request )
{
    if ( request.url().toString().startsWith( "http" ) ) { return true; }

    request.setUrl( QString( "%1%2" ).arg( httpDomain, request.url().toString() ) );

    return true;
}

bool IMHttpRequest::requestDataSerializer(
        QByteArray &data )
{
    WTHttpRequest::requestDataSerializer(data);
    //    data = QJsonDocument( QJsonObject::fromVariantMap( requestData().value< QJSValue >().toVariant().toMap() ) ).toJson( QJsonDocument::Compact );

    return true;
}

bool IMHttpRequest::responseDataParser(
        const QPointer< QNetworkReply > &reply,
        const QByteArray &               sourceData,
        QVariant &                       targetData,
        QString &                        message ,int & code )
{
    Q_UNUSED( reply );
    QJsonObject replyData;
    targetData=sourceData;

    replyData = QJsonDocument::fromJson( sourceData ).object();
    if(this->enabledLog())
    {
        foreach (auto h, reply->rawHeaderList()) {
            qDebug()<<"===responseData===headers====="<<h<<":"<< reply->rawHeader(h);
        }

    }



//    if ( replyData.isEmpty() )
//    {
//        message = "reply data is null (1)";
//        return false;
//    }

    if ( this->noReplyCode() )
    {
        return true;
    }

    if ( !replyData.contains( "status" ) )
    {
        message = "reply code not exists";
        return false;
    }
    code= replyData[ "status" ].toInt() ;
    if (code != 200)
    {
        message = replyData[ "message" ].toString();
        return false;
    }

    if ( !replyData.contains( "data" ) )
    {
        //message = "reply data is null (2)";
        return true;
    }

    if ( replyData[ "data" ].isNull())
    {
//        targetData = replyData[ "data" ].toObject();
    }
    else if ( replyData[ "data" ].isObject() )
    {
        targetData = replyData[ "data" ].toObject();
    }
    else if ( replyData[ "data" ].isArray() )
    {
        targetData = replyData[ "data" ].toArray();
    }
    else
    {
        message = "reply data type error";
        return false;
    }

    return true;
}


// HttpHelper
HttpHelper::HttpHelper()
{ }

HttpHelper::~HttpHelper()
{ }

HttpHelper *HttpHelper::instance()
{
    static HttpHelper global;
    return &global;
}


QObject *HttpHelper::createIMRequest()
{
    return new IMHttpRequest;
}

